

<!DOCTYPE html>
<html lang="zh-CN">

<head><meta name="generator" content="Hexo 3.9.0">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>使用 ProcessLifecycle 优雅地监听应用前后台切换 - 杨云召 | 博客</title>
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
  <meta name="google" content="notranslate">

  
  
  <meta name="description" content="使用 ProcessLifecycle 优雅地监听应用..."> 
  
  <meta name="author" content="杨云召（Flywith24）"> 

  
    <link rel="icon" href="/images/icons/favicon-16x16.png" type="image/png" sizes="16x16">
  
  
    <link rel="icon" href="/images/icons/favicon-32x32.png" type="image/png" sizes="32x32">
  
  
    <link rel="apple-touch-icon" href="/images/icons/apple-touch-icon.png" sizes="180x180">
  
  
    <meta rel="mask-icon" href="/images/icons/stun-logo.svg" color="#333333">
  
  
    <meta rel="msapplication-TileImage" content="/images/icons/favicon-144x144.png">
    <meta rel="msapplication-TileColor" content="#000000">
  

  <link rel="stylesheet" href="/css/style.css">

  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1445822_hhodbqn7tit.css">
  

  
  
  <link rel="stylesheet" href="https://cdn.bootcss.com/fancybox/3.5.7/jquery.fancybox.min.css">
  

  
  
  <link rel="stylesheet" href="https://cdn.bootcss.com/highlight.js/9.18.1/styles/xcode.min.css">
  

  <script>
    var CONFIG = window.CONFIG || {};
    var ZHAOO = window.ZHAOO || {};
    CONFIG = {
      isHome: false,
      fancybox: true,
      pjax: false,
      lazyload: {
        enable: true,
        loadingImage: '/images/theme/loading.gif',
      },
      donate: {
        enable: true,
        alipay: 'https://gitee.com/flywith24/Album/raw/master/img/20201015113301.jpg',
        wechat: 'https://gitee.com/flywith24/Album/raw/master/img/20201015112814.png'
      },
      motto: {
        api: '',
        default: '不人云亦云，只求接近真相.'
      },
      galleries: {
        enable: true
      },
      fab: {
        enable: true,
        alwaysShow: false
      },
      carrier: {
        enable: true
      },
      daovoice: {
        enable: true
      }
    }
  </script>

  

  
</head></html>
<body class="lock-screen">
  <div class="loading"></div>
  


<nav class="navbar">
  <div class="left"></div>
  <div class="center">使用 ProcessLifecycle 优雅地监听应用前后台切换</div>
  <div class="right">
    <i class="iconfont iconmenu j-navbar-menu"></i>
  </div>
</nav>

  <nav class="menu">
  <div class="menu-wrap">
    <div class="menu-close">
      <i class="iconfont iconbaseline-close-px"></i>
    </div>
    <ul class="menu-content">
      
      
      
      
      <li class="menu-item"><a href="/ " class="underline"> 首页</a></li>
      
      
      
      
      <li class="menu-item"><a href="/archives " class="underline"> 归档</a></li>
      
      
      
      
      <li class="menu-item"><a href="/tags " class="underline"> 标签</a></li>
      
      
      
      
      <li class="menu-item"><a href="/categories " class="underline"> 分类</a></li>
      
      
      
      
      <li class="menu-item"><a href="/about " class="underline"> 关于</a></li>
      
    </ul>
    <div class="menu-copyright"><p>Copyright© 2017 - 2020 ❤️ Flywith24</p></div>
  </div>
</nav>
  <main id="main">
  <div class="container" id="container">
    <article class="article">
  <div class="wrap">
    <section class="head">
  <img   class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20201015104945.png" src=""  draggable="false">
  <div class="head-mask">
    <h1 class="head-title">使用 ProcessLifecycle 优雅地监听应用前后台切换</h1>
    <div class="head-info">
      <span class="post-info-item"><i class="iconfont iconcalendar"></i>七月 01, 2020</span
        class="post-info-item">
      
      <span class="post-info-item"><i class="iconfont iconfont-size"></i>5981</span>
    </div>
  </div>
</section>
    <section class="main">
      <section class="content">
        <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>很高兴见到你，又来到了「奇技淫巧」系列，本系列介绍一些「骚操作」，可能不适合用于生产，但可以开拓思路</p>
<p>前些天在群里看到有人讨论通过维护 activity 栈来监听程序前后台切换的问题。其实单纯监听程序的前后台切换完全不需要维护 activity 栈，而现在比较主流的做法是使用 <code>registerActivityLifecycleCallbacks</code>。而今天我来介绍一下使用 ProcessLifecycleOwner 来实现这一功能</p>
<h2 id="lifecycle-process-库"><a href="#lifecycle-process-库" class="headerlink" title="lifecycle-process 库"></a>lifecycle-process 库</h2><p>Android Jetpack Lifecycle 组件有一个可选库：lifecycle-process，它可以为整个 app 进程提供一个 ProcessLifecycleOwner</p>
<p><img class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20200701110431.png" src="/2020/07/01/Tips-ProcessLifecycle/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt="lifecycle-process 引入"></p>
<p>该库十分简单，只有四个文件</p>
<p><img class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20200701110904.png" src="/2020/07/01/Tips-ProcessLifecycle/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt="lifecycle-process"></p>
<p><code>ProcessLifecycleOwnerInitializer</code> 借助 ContentProvider 拿到 Context，用于初始化操作</p>
<p><img class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20200701112017.png" src="/2020/07/01/Tips-ProcessLifecycle/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt="init"></p>
<p><code>EmptyActivityLifecycleCallbacks</code> 为 <code>Application.ActivityLifecycleCallbacks</code> 的实现类，内部为空实现</p>
<p><img class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20200701112219.png" src="/2020/07/01/Tips-ProcessLifecycle/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt="EmptyActivityLifecycleCallbacks"></p>
<p><code>LifecycleDispatcher</code> 通过 ReportFragment 来 hook 宿主的生命周期事件</p>
<p><img class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20200701113143.png" src="/2020/07/01/Tips-ProcessLifecycle/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt></p>
<p>核心逻辑都在 ProcessLifecycleOwner 中</p>
<p><img class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20200701113324.png" src="/2020/07/01/Tips-ProcessLifecycle/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt="ProcessLifecycleOwner "></p>
<p>该类提供了整个 app 进程的 lifecycle</p>
<p>可以将其视为所有 activity 的 LifecycleOwner ，其中 <a href="https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_CREATE" target="_blank" rel="noopener">Lifecycle.Event.ON_CREATE</a> 只会分发一次，而 <a href="https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_DESTROY" target="_blank" rel="noopener">Lifecycle.Event.ON_DESTROY</a> 则永远不会分发</p>
<p>其它的生命周期事件将按以下规则分发：</p>
<p><code>ProcessLifecycleOwner</code> 会分发 <a href="https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_START" target="_blank" rel="noopener">Lifecycle.Event.ON_START</a> 和 <a href="https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_RESUME" target="_blank" rel="noopener">Lifecycle.Event.ON_RESUME</a> 事件（在第一个 activity 移动到这些事件时）</p>
<p><a href="https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_PAUSE" target="_blank" rel="noopener">Lifecycle.Event.ON_PAUSE</a> 与 <a href="https://developer.android.com/reference/androidx/lifecycle/Lifecycle.Event#ON_STOP" target="_blank" rel="noopener">Lifecycle.Event.ON_STOP</a> 会在最后一个 activity 移动到这些状态后 <strong>延迟</strong> 分发，该延迟足够长，以确保由于配置更改等操作重建 activity 后不会分发任何事件</p>
<p>对于监听应用在前后台切换且不需要毫秒级的精度的场景，这十分有用</p>
<h2 id="ProcessLifecycleOwner-源码解析"><a href="#ProcessLifecycleOwner-源码解析" class="headerlink" title="ProcessLifecycleOwner  源码解析"></a>ProcessLifecycleOwner  源码解析</h2><p>根据上图我们得知 <code>ProcessLifecycleOwner</code>  实现了 LifecycleOwner 接口</p>
<p>由于在 <code>ProcessLifecycleOwnerInitializer</code> 中初始化时传入了 Context，因此 <code>ProcessLifecycleOwner</code>  在 attach 方法中借助 Context 拿到了 Application 实例，并调用了 <code>registerActivityLifecycleCallbacks</code></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">attach</span><span class="hljs-params">(Context context)</span> </span>&#123;<br>    mHandler = <span class="hljs-keyword">new</span> Handler();<br>    mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);<br>    Application app = (Application) context.getApplicationContext();<br>    app.registerActivityLifecycleCallbacks(<span class="hljs-keyword">new</span> EmptyActivityLifecycleCallbacks() <br>        <span class="hljs-meta">@RequiresApi</span>(<span class="hljs-number">29</span>)<br>        <span class="hljs-meta">@Override</span><br>        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onActivityPreCreated</span><span class="hljs-params">(@NonNull Activity activity,<br>                @Nullable Bundle savedInstanceState)</span> </span>&#123;<br>            <span class="hljs-comment">//我们需要 ProcessLifecycleOwner 刚好在第一个 activity 的 LifecycleOwner started/resumed 之前获取 ON_START 和 ON_RESUME。</span><br>            <span class="hljs-comment">//activity 的 LifecycleOwner 通过在 onCreate() 中添加 activity 注册的 callback 来获取 started/resumed 状态。</span><br>            <span class="hljs-comment">//通过在 onActivityPreCreated() 中添加我们自己的 activity 注册的 callback，我们首先获得了回调，同时与 Activity 的 onStart()/ onResume()回调相比仍具有正确的相对顺序</span><br>  		   <br>            activity.registerActivityLifecycleCallbacks(<span class="hljs-keyword">new</span> EmptyActivityLifecycl<br>                <span class="hljs-meta">@Override</span><br>                <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onActivityPostStarted</span><span class="hljs-params">(@NonNull Activity activity)</span> </span>&#123;<br>                    activityStarted();<br>                &#125;<br>                <span class="hljs-meta">@Override</span><br>                <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onActivityPostResumed</span><span class="hljs-params">(@NonNull Activity activity)</span> </span>&#123;<br>                    activityResumed();<br>                &#125;<br>            &#125;);<br>        &#125;<br>        <span class="hljs-meta">@Override</span><br>        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onActivityCreated</span><span class="hljs-params">(Activity activity, Bundle savedInstanceStat<br>            //仅在API <span class="hljs-number">29</span> 之前使用 ReportFragment，在此之后，我们可以使用在 onActivityPreCreated()</span> 中注册的 onActivityPostStarted 和 onActivityPostResumed 回调<br>            <span class="hljs-title">if</span> <span class="hljs-params">(Build.VERSION.SDK_INT &lt; <span class="hljs-number">29</span>)</span> </span>&#123;<br>                ReportFragment.get(activity).setProcessListener(mInitializationLi<br>            &#125;<br>        &#125;<br>        <span class="hljs-meta">@Override</span><br>        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onActivityPaused</span><span class="hljs-params">(Activity activity)</span> </span>&#123;<br>            activityPaused();<br>        &#125;<br>        <span class="hljs-meta">@Override</span><br>        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onActivityStopped</span><span class="hljs-params">(Activity activity)</span> </span>&#123;<br>            activityStopped();<br>        &#125;<br>    &#125;);<br>&#125;<br></code></pre></td></tr></table></figure>

<p>内部维护了 Started 和 Resumed 的数量</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> mStartedCounter = <span class="hljs-number">0</span>;<br><span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> mResumedCounter = <span class="hljs-number">0</span>;<br><span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> mPauseSent = <span class="hljs-keyword">true</span>;<br><span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> mStopSent = <span class="hljs-keyword">true</span>;<br></code></pre></td></tr></table></figure>

<p>并在 activityStarted 和 activityResumed 方法中对 这两个数值进行 ++，并更改 lifecycle 状态</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">activityStarted</span><span class="hljs-params">()</span> </span>&#123;<br>    mStartedCounter++;<br>    <span class="hljs-keyword">if</span> (mStartedCounter == <span class="hljs-number">1</span> &amp;&amp; mStopSent) &#123;<br>        mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);<br>        mStopSent = <span class="hljs-keyword">false</span>;<br>    &#125;<br>&#125;<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">activityResumed</span><span class="hljs-params">()</span> </span>&#123;<br>    mResumedCounter++;<br>    <span class="hljs-keyword">if</span> (mResumedCounter == <span class="hljs-number">1</span>) &#123;<br>        <span class="hljs-keyword">if</span> (mPauseSent) &#123;<br>            mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);<br>            mPauseSent = <span class="hljs-keyword">false</span>;<br>        &#125; <span class="hljs-keyword">else</span> &#123;<br>            mHandler.removeCallbacks(mDelayedPauseRunnable);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>在 activityPaused 和 activityStopped 方法对这两个数值进行 –</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">activityPaused</span><span class="hljs-params">()</span> </span>&#123;<br>    mResumedCounter--;<br>    <span class="hljs-keyword">if</span> (mResumedCounter == <span class="hljs-number">0</span>) &#123;<br>        mHandler.postDelayed(mDelayedPauseRunnable, TIMEOUT_MS);<br>    &#125;<br>&#125;<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">activityStopped</span><span class="hljs-params">()</span> </span>&#123;<br>    mStartedCounter--;<br>    dispatchStopIfNeeded();<br>&#125;<br></code></pre></td></tr></table></figure>

<p>而在这里我们看到了上文提到的延迟操作</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 使用 handler 进行延迟操作</span><br>mHandler.postDelayed(mDelayedPauseRunnable, TIMEOUT_MS);<br><br><span class="hljs-comment">// 延迟 700 ms</span><br><span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> TIMEOUT_MS = <span class="hljs-number">700</span>; <span class="hljs-comment">//mls</span><br><br><span class="hljs-keyword">private</span> Runnable mDelayedPauseRunnable = <span class="hljs-keyword">new</span> Runnable() &#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-comment">// 根据需要分发事件</span><br>        dispatchPauseIfNeeded();<br>        dispatchStopIfNeeded();<br>    &#125;<br>&#125;;<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">dispatchPauseIfNeeded</span><span class="hljs-params">()</span> </span>&#123;<br>    <span class="hljs-keyword">if</span> (mResumedCounter == <span class="hljs-number">0</span>) &#123;<br>        mPauseSent = <span class="hljs-keyword">true</span>;<br>        mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE);<br>    &#125;<br>&#125;<br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">dispatchStopIfNeeded</span><span class="hljs-params">()</span> </span>&#123;<br>    <span class="hljs-keyword">if</span> (mStartedCounter == <span class="hljs-number">0</span> &amp;&amp; mPauseSent) &#123;<br>        mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);<br>        mStopSent = <span class="hljs-keyword">true</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>源码就解析到这里，接下来我们看看如何使用吧</p>
<h2 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h2><p>首先引入该库</p>
<figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs groovy">implementation <span class="hljs-string">"androidx.lifecycle:lifecycle-process:2.3.0-alpha05"</span><br></code></pre></td></tr></table></figure>

<p>由于我们要自定义 lifecycleObserver，因此还需引入</p>
<figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs groovy">implementation <span class="hljs-string">"androidx.lifecycle:lifecycle-common-java8:2.3.0-alpha05"</span><br></code></pre></td></tr></table></figure>

<p>首先创建 <code>ProcessLifecycleObserver</code> 类，实现 <code>DefaultLifecycleObserver</code> 接口，在相应的生命周期中打印 log</p>
<p>接着在自定义 Application 中加入</p>
<p><img class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20200701121028.png" src="/2020/07/01/Tips-ProcessLifecycle/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt></p>
<p>这样便完成了！</p>
<p><img class="lazyload" data-original="https://user-gold-cdn.xitu.io/2020/7/1/173089686a09e8f3?w=1854&h=999&f=gif&s=2823298" src="/2020/07/01/Tips-ProcessLifecycle/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt="演示"></p>
<p><a href="https://github.com/Flywith24/ProcessLifecycle-Demo" target="_blank" rel="noopener">Demo 在这里</a></p>
<h2 id="系列文章"><a href="#系列文章" class="headerlink" title="系列文章"></a>系列文章</h2><ul>
<li><a href="https://juejin.im/post/5e481a28f265da570b3f235c" target="_blank" rel="noopener">【奇技淫巧】AndroidStudio Nexus3.x搭建Maven私服遇到问题及解决方案</a></li>
</ul>
<ul>
<li><a href="https://juejin.im/post/5e22c2ce6fb9a02ff67d41c3" target="_blank" rel="noopener">【奇技淫巧】什么？项目里gradle代码超过200行了！你可能需要 Kotlin+buildSrc Plugin</a></li>
</ul>
<ul>
<li><a href="https://juejin.im/post/5e481a28f265da570b3f235c" target="_blank" rel="noopener">【奇技淫巧】gradle依赖查找太麻烦？这个插件可能帮到你</a></li>
</ul>
<ul>
<li><a href="https://juejin.im/post/5e967f35f265da47d77cd4c3" target="_blank" rel="noopener">【奇技淫巧】Android组件化不使用 Router 如何实现组件间 activity 跳转</a></li>
</ul>
<ul>
<li><a href="https://juejin.im/post/5ebdfb0b6fb9a0436153db22" target="_blank" rel="noopener">【奇技淫巧】新的图片加载库？基于Kotlin协程的图片加载库——Coil</a></li>
</ul>
<ul>
<li><a href="https://juejin.im/post/5ec50ae46fb9a047a862124f" target="_blank" rel="noopener">【奇技淫巧】使用 Navigation + Dynamic Feature Module 实现模块化</a></li>
</ul>
<ul>
<li><a href="https://juejin.im/post/5ecde219e51d457841190d08" target="_blank" rel="noopener">【奇技淫巧】除了 buildSrc 还能这样统一配置依赖版本？巧用 includeBuild</a></li>
</ul>
<ul>
<li><a href="https://juejin.im/post/5ed9c92ce51d45789b35afa9" target="_blank" rel="noopener">【奇技淫巧】巧用 kotlin 扩展函数和 typealias 封装 带网络状态和解决「粘性」事件的 LiveData</a></li>
</ul>
<h2 id="关于我"><a href="#关于我" class="headerlink" title="关于我"></a>关于我</h2><p>我是 <a href="https://flywith24.gitee.io/">Flywith24</a>，我的博客内容已经分类整理 <a href="https://github.com/Flywith24/BlogList" target="_blank" rel="noopener">在这里</a>，点击右上角的 Watch 可以及时获取我的文章更新哦 😉</p>
<ul>
<li><p><a href="https://juejin.im/user/57c7f6870a2b58006b1cfd6c" target="_blank" rel="noopener">掘金</a></p>
</li>
<li><p><a href="https://xiaozhuanlan.com/detail" target="_blank" rel="noopener">小专栏</a></p>
</li>
<li><p><a href="https://github.com/Flywith24" target="_blank" rel="noopener">Github</a></p>
</li>
</ul>
<p><img class="lazyload" data-original="https://user-gold-cdn.xitu.io/2020/6/26/172ee567fb4fbf7e?w=1954&h=624&f=jpeg&s=115362" src="/2020/07/01/Tips-ProcessLifecycle/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt></p>
      </section>
      <section class="extra">
        
        <ul class="copyright">
  
  <li><strong>本文作者：</strong>杨云召（Flywith24）</li>
  <li><strong>本文链接：</strong><a href="https://flywith24.gitee.io/2020/07/01/Tips-ProcessLifecycle/index.html">https://flywith24.gitee.io/2020/07/01/Tips-ProcessLifecycle/index.html</a></li>
  <li><strong>版权声明：</strong>本博客所有文章均采用<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh"
      rel="external nofollow" target="_blank"> BY-NC-SA </a>许可协议，转载请注明出处！</li>
  
</ul>
        
        
        <section class="donate">
  <div class="qrcode">
    <img   class="lazyload" data-original="https://gitee.com/flywith24/Album/raw/master/img/20201015113301.jpg" src="" >
  </div>
  <div class="icon">
    <a href="javascript:;" id="alipay"><i class="iconfont iconalipay"></i></a>
    <a href="javascript:;" id="wechat"><i class="iconfont iconwechat-fill"></i></a>
  </div>
</section>
        
        
  <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Tips/">Tips</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/奇技淫巧/">奇技淫巧</a></li></ul>

        
<nav class="nav">
  
    <a href="/2020/08/18/Framework-Compile/"><i class="iconfont iconleft"></i>源码编译与烧写</a>
  
  
    <a href="/2020/05/20/Tips-Navagion+DynamicFeatureModule/">使用 Navigation + Dynamic Feature Module 实现模块化<i class="iconfont iconright"></i></a>
  
</nav>

      </section>
      
      <section class="comments">
  
  <div class="btn" id="comments-btn">查看评论</div>
  
  
<div id="valine"></div>
<script defer src="//unpkg.com/valine/dist/Valine.min.js"></script>
<script>
  window.onload = function () {
    var loadValine = function () {
      new Valine({
        el: '#valine',
        app_id: "SHA3gzCcxFlqaPcGRnX4UgET-gzGzoHsz",
        app_key: "OYo7ITS4AzTvBUvb1yIY8oaf",
        placeholder: "期待你的留言",
        avatar: "mp",
        pageSize: "10",
        lang: "zh-CN",
      });
    }
    if ( true ) {
      $("#comments-btn").on("click", function () {
        $(this).hide();
        loadValine();
      });
    } else {
      loadValine();
    }
  };
</script>

</section>
      
    </section>
  </div>
</article>
  </div>
</main>
  <footer class="footer">
  <div class="footer-social">
    
    
    
    
    
    <a href="tencent://message/?Menu=yes&uin=1032367864 " target="_blank" onMouseOver="this.style.color= '#12B7F5'"
      onMouseOut="this.style.color='#33333D'">
      <i class="iconfont footer-social-item  iconQQ "></i>
    </a>
    
    
    
    
    
    <a href="javascript:; " target="_blank" onMouseOver="this.style.color= '#8bc34a'"
      onMouseOut="this.style.color='#33333D'">
      <i class="iconfont footer-social-item  iconwechat-fill "></i>
    </a>
    
    
    
    
    
    <a href="https://flywith24.gitee.io/about/ " target="_blank" onMouseOver="this.style.color= '#d32f2f'"
      onMouseOut="this.style.color='#33333D'">
      <i class="iconfont footer-social-item  iconheart "></i>
    </a>
    
    
    
    
    
    <a href="https://github.com/Flywith24 " target="_blank" onMouseOver="this.style.color= '#24292E'"
      onMouseOut="this.style.color='#33333D'">
      <i class="iconfont footer-social-item  icongithub-fill "></i>
    </a>
    
    
    
    
    
    <a href="mailto:youngyunzhao@163.com " target="_blank" onMouseOver="this.style.color='#FFBE5B'"
      onMouseOut="this.style.color='#33333D'">
      <i class="iconfont footer-social-item  iconmail"></i>
    </a>
    
  </div>
  <div class="footer-copyright"><p>Copyright© 2017 - 2020 ❤️ Flywith24</p></div>
</footer>
  
      <div class="fab fab-plus">
    <i class="iconfont iconplus"></i>
  </div>
  
  <div class="fab fab-daovoice">
    <i class="iconfont iconcomment"></i>
  </div>
  
  <div class="fab fab-up">
    <i class="iconfont iconcaret-up"></i>
  </div>
  
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":150,"height":300},"mobile":{"show":"tfalse"},"log":false});</script></body>

<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>




<script src="https://cdn.bootcdn.net/ajax/libs/jquery.lazyload/1.9.1/jquery.lazyload.min.js"></script>




<script src="https://cdn.bootcss.com/fancybox/3.5.7/jquery.fancybox.min.js"></script>




<script src="/js/utils.js"></script>
<script src="/js/modules.js"></script>
<script src="/js/zui.js"></script>
<script src="/js/script.js"></script>




<script>
  (function (i, s, o, g, r, a, m) {
    i["DaoVoiceObject"] = r;
    i[r] = i[r] || function () {
      (i[r].q = i[r].q || []).push(arguments)
    }, i[r].l = 1 * new Date();
    a = s.createElement(o), m = s.getElementsByTagName(o)[0];
    a.async = 1;
    a.src = g;
    a.charset = "utf-8";
    m.parentNode.insertBefore(a, m)
  })(window, document, "script", ('https:' == document.location.protocol ? 'https:' : 'http:') +
    "//widget.daovoice.io/widget/0f81ff2f.js", "daovoice")
  daovoice('init', {
    app_id: "7785620b"
  }, {
    launcher: {
      disableLauncherIcon: true,
    },
  });
  daovoice('update');
</script>



<script>
  (function () {
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
      bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    } else {
      bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
  })();
</script>


<script>
  var _hmt = _hmt || [];
  (function () {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?4c204d8bc027a0455b5fc642ac334ca8";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
</script>










</html>